package com.ztc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ztc.bean.StoreModel;
import com.ztc.service.StoreService;

/**
 * @类名：StoreController.java
 * @作者：李晓平
 * @时间：2018年7月25日 下午2:03:34
 * @描述：收藏功能控制类
 */
@CrossOrigin
@RestController
public class StoreController {
	private Logger logger = Logger.getLogger(StoreController.class);
	
	@Autowired
	private StoreService storeService;
	
	/**
	 * 查询收藏列表
	 * @param storeModel
	 * @return
	 */
	@RequestMapping("/queryStoreList")
	public Map<String, Object> queryStoreList(StoreModel store) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean success = true;
		String message = "";
		try {
			List<StoreModel> storeList = storeService.queryStoreList(store);
			resultMap.put("result", storeList);
		} catch (IllegalArgumentException ie) {
			success = false;
			message = ie.getMessage();
			logger.error(message, ie);
		} catch (Exception e) {
			success = false;
			message = "未知异常，查询收藏列表失败";
			logger.error(message, e);
		}
		resultMap.put("success", success);
		resultMap.put("message", message);
		
		return resultMap;
	}
	
	/**
	 * 查询收藏明细
	 * @param id 收藏ID
	 * @return
	 */
	@RequestMapping("/queryOneStore")
	public Map<String, Object> queryOneStore(Integer id, Integer projectId, Integer authId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean success = true;
		String message = "";
		try {
			if(id == null) {
				throw new IllegalArgumentException("请输入收藏信息ID");
			}
			if(projectId == null) {
				throw new IllegalArgumentException("请输入项目ID");
			}
			if(authId == null) {
				throw new IllegalArgumentException("请输入权限ID");
			}
			StoreModel store = storeService.queryOneStore(id, projectId, authId);
			resultMap.put("result", store);
		} catch (IllegalArgumentException ie) {
			success = false;
			message = ie.getMessage();
			logger.error(message, ie);
		} catch (Exception e) {
			success = false;
			message = "未知异常，查询收藏明细失败";
			logger.error(message, e);
		}
		resultMap.put("success", success);
		resultMap.put("message", message);
		
		return resultMap;
	}
	
	/**
	 * 添加收藏
	 * @param storeModel
	 * @return
	 */
	@RequestMapping("/addStore")
	public Map<String, Object> addStore(StoreModel store) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean success = true;
		String message = "";
		try {
			if(StringUtils.isBlank(store.getTitle())) {
				throw new IllegalArgumentException("标题不能为空");
			}
			if(StringUtils.isBlank(store.getContent())) {
				throw new IllegalArgumentException("内容不能为空");
			}
			storeService.addStore(store);
		} catch (IllegalArgumentException ie) {
			success = false;
			message = ie.getMessage();
			logger.error(message, ie);
		} catch (Exception e) {
			success = false;
			message = "未知异常，添加收藏失败";
			logger.error(message, e);
		}
		resultMap.put("success", success);
		resultMap.put("message", message);
		
		return resultMap;
	}
	
	/**
	 * 删除收藏
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteStore")
	public Map<String, Object> deleteStore(Integer id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean success = true;
		String message = "";
		try {
			if(id == null) {
				throw new IllegalArgumentException("请输入收藏信息ID");
			}
			storeService.deleteStore(id);
		} catch (IllegalArgumentException ie) {
			success = false;
			message = ie.getMessage();
			logger.error(message, ie);
		} catch (Exception e) {
			success = false;
			message = "未知异常，删除收藏失败";
			logger.error(message, e);
		}
		resultMap.put("success", success);
		resultMap.put("message", message);
		
		return resultMap;
	}
}